﻿using BusinessLogicLayer;
using DevComponents.DotNetBar.Controls;
using Equin.ApplicationFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PresentationLayer
{
    public delegate void CapNhatDKTangCa(List<DKTangCa> np);

    public partial class frmDangKyTangCa : Form
    {
        public List<DKTangCa> DSNhanVienDuocChon { get; set; }
        private List<NhanVien> dsNhanVien = null;
        private TangCaManager manager = new TangCaManager();
        private CapNhatDKTangCa CapNhat;
        public string MaTangCa { get; set; }
        public frmDangKyTangCa()
        {
            InitializeComponent();
        }

        public frmDangKyTangCa(DateTime NgayBD,DateTime NgayKT,CapNhatDKTangCa capnhat,List<DKTangCa> dsdk, string MaTangCa)
        {
            InitializeComponent();
            DSNhanVienDuocChon = dsdk;
            dtiNgay.MaxDate = NgayKT;
            dtiNgay.MinDate = NgayBD;
            CapNhat = capnhat;
            this.MaTangCa = MaTangCa;
        }

        private void LoadForm()
        {
            dtiNgay.Value = dtiNgay.MinDate;
        }

        private void frmDangKyTangCa_Load(object sender, EventArgs e)
        {
            dgvDSNhanVien.AutoGenerateColumns = false;
            LoadForm();
        }

        private void dtiNgay_Validated(object sender, EventArgs e)
        {
            if (dtiNgay.Value >= dtiNgay.MinDate && dtiNgay.Value <= dtiNgay.MaxDate)
            {
                ActionResultAsList<NhanVien> rs = null;
                rs = manager.LayDanhSachNhanVien(dtiNgay.Value, MaTangCa);
                if (rs.Success)
                {
                    dgvDSNhanVien.DataSource = null;
                    dsNhanVien = rs.ObjectList;
                    foreach (DKTangCa dk in DSNhanVienDuocChon)
                    {
                        NhanVien d = dsNhanVien.Find(s => s.MaNhanVien == dk.MaNhanVien && dtiNgay.Value == dk.NgayDangKy);
                        if (d != null)
                            d.DaChonNV = true;
                    }
                    dgvDSNhanVien.DataSource = new BindingListView<NhanVien>(dsNhanVien);
                }
            }
            else
            {
                MessageBox.Show("Chọn ngày từ khoảng " + dtiNgay.MinDate.Day + "/" + dtiNgay.MinDate.Month + "/" + dtiNgay.MinDate.Year +
                                                    "-->" + dtiNgay.MaxDate.Day + "/" + dtiNgay.MaxDate.Month + "/" + dtiNgay.MaxDate.Year + "!!!",
                                                    "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dgvDSNhanVien_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex != -1)
            {
                if (dgvDSNhanVien.SelectedRows.Count > 0)
                {
                    DataGridViewCheckBoxXCell checkcell = dgvDSNhanVien.SelectedRows[0].Cells["DaChonNV"] as DataGridViewCheckBoxXCell;
                    if (Convert.ToBoolean(checkcell.Value) == true)
                    {
                        checkcell.Value = false;
                        dsNhanVien.Find(s => s.MaNhanVien.CompareTo(dgvDSNhanVien.SelectedRows[0].Cells["MaNhanVien"].Value) == 0).DaChonNV = false;
                    }
                    else
                    {
                        checkcell.Value = true;
                        dsNhanVien.Find(s => s.MaNhanVien.CompareTo(dgvDSNhanVien.SelectedRows[0].Cells["MaNhanVien"].Value) == 0).DaChonNV = true;
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private List<DKTangCa> LayDanhSach()
        {
            List<DKTangCa> ds = new List<DKTangCa>();
            foreach (NhanVien nv in dsNhanVien)
            {
                if (nv.DaChonNV == true)
                {
                    DKTangCa dk = new DKTangCa();
                    dk.MaTangCa = MaTangCa;
                    dk.NgayDangKy = dtiNgay.Value;
                    dk.MaNhanVien = nv.MaNhanVien;
                    dk.NhanVien = nv;
                    ds.Add(dk);
                }
            }
            return ds;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            List<DKTangCa> ds = DSNhanVienDuocChon.FindAll(s => s.NgayDangKy == dtiNgay.Value);
            foreach (DKTangCa dk in ds)
            {
                DSNhanVienDuocChon.Remove(dk);
            }
            ds = LayDanhSach();
            foreach (DKTangCa dk in ds)
            {
                DSNhanVienDuocChon.Add(dk);
            }
            CapNhat(DSNhanVienDuocChon);
        }
    }
}
